from selenium import webdriver
from urllib import request
from bs4 import BeautifulSoup
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
import csv
import codecs
import sys
import MySQLdb


def getData(queryName) :

  caps = webdriver.DesiredCapabilities().FIREFOX

  caps["marionette"] = False

  binary = FirefoxBinary(r'H:\Program Files (x86)\Mozilla Firefox\firefox.exe')

  fp = webdriver.FirefoxProfile

  driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps)

  driver.get(
    'https://search.jd.com/Search')

  # 获取到文本框
  keys_value = driver.find_element_by_id('key')
  # 根据不同名称搜索
  keys_value.send_keys(queryName)
  # 进行搜索点击
  driver.find_element_by_id('search-2014').find_element_by_tag_name('button').click()
  # 查询分页信息
  time.sleep(2)
  page = driver.find_element_by_css_selector('div.p-wrap')


  if len(page.text) > 0  :
      pageSum = page.find_element_by_class_name('p-skip').find_element_by_tag_name('b').text  ## 分页总数
  else:
      pageSum = 1


  for i in range(0, int(pageSum)):
    driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')  ## 下滑到底部
    time.sleep(5)
    product_list = driver.find_elements_by_css_selector('li.gl-item')  ##商品列表
    try:

        list = [];
        for product in product_list:
            # 价格
            price_tag = product.find_element_by_class_name('p-price')
            price = price_tag.find_element_by_tag_name('i')
            # 商品名
            name_tag = product.find_element_by_class_name('p-name')
            title = name_tag.find_element_by_tag_name('em')
            # 评价人数
            name_commit = product.find_element_by_class_name('p-commit')
            commit = name_commit.find_element_by_tag_name('a')
            # 图标内容
            # 查询class 名称
            icons_tag = product.find_element_by_class_name('p-icons')

            # 商家
            # 查询class 名称
            priduct_show = product.find_element_by_class_name('p-show')

            # class 下面的标签
            if len(icons_tag.text) > 0 :
              icon = icons_tag.find_element_by_tag_name('i')

            if float(price.text) <= 3500 :
                print('内容：' + title.text + '价格：' + price.text )
                list.append([title.text,price.text,icon.text,commit.text,priduct_show.text])
        if i == 10:
             continue;
        driver.find_element_by_css_selector('div.p-wrap').find_element_by_class_name('pn-next').click()
    except Exception as e:
        print(e)
    writerCsv(list)
    insert_mysql(list)



def writerCsv(lists):

    if len(lists) :
        # 表格文件头
        headers = ['商品名称', '价格', '标签', '评价人数']
        rows = []
        # 遍历数据对象
        for list in lists:
            rows.append([
                [list[0], list[1], list[2], list[3]],
            ])

        # 写入文件
        with open('./5ly.csv', 'w', encoding='UTF-8' ,newline='')as f:
            f_csv = csv.writer(f)
            # f_csv.writerow(codecs.BOM_UTF8)
            f_csv.writerow(headers)
            f_csv.writerows(rows)
        f.close()
    else:
        return

def insert_mysql(data):
    conn = MySQLdb.connect(host='47.106.73.21',user='root',password='th.980224',db='reptiles')
    cur = conn.cursor()
    cur = cur.executemany('insert into rep_jd_product (p_name , p_img , p_price ,p_commit,p_shop,p_url) values (%s,%s,%s,%s,%s,%s)',data)
    cur.close()
    conn.commit()
    conn.close()


if __name__ == '__main__':
     getData('茅台')


